package com.zengdada.authority.controller;

import com.zengdada.authority.service.AppDictionaryAuthorityService;
import com.zengdada.authority.utils.JsonMessage;
import com.zengdada.authority.utils.where.Where;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;

import static com.zengdada.authority.utils.JsonMessage.fail;
import static com.zengdada.authority.utils.JsonMessage.success;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2018/12/7 14:26
 * @Description:
 */
@Api(tags = "字典表控制器")
@Controller
@RequestMapping("/authority/dic")
public class AppDictionaryAuthorityController {

    @Autowired
    @Qualifier("appDictionaryAuthorityServiceImpl")
    AppDictionaryAuthorityService appDictionaryAuthorityService;

    @ApiOperation(value = "递归获取树信息", notes = "更具开始条件递归查询数据并组装成树结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startIds", value = "递归开始的ID（可以是多个用“,”隔开）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "retainHead", value = "是否保留开始的信息，默认：false", dataType = "boolean"),
            @ApiImplicitParam(name = "childrenName", value = "子级属性的名称，默认：children", dataType = "String"),
    })
    @RequestMapping(value = "/recursion_list_tree.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage recursionListTree(@RequestParam("startIds") String startIds
            , @RequestParam(defaultValue = "false") boolean retainHead
            , @RequestParam(defaultValue = "children") String childrenName) {
        try {
            String[] split = startIds.split(",");
            return success(appDictionaryAuthorityService.recursiveQueryTree(Arrays.asList(split), retainHead, childrenName));
        } catch (Exception e) {
            return fail("数据获取失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "递归获取信息", notes = "更具开始条件递归查询数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startIds", value = "递归开始的ID（可以是多个用“,”隔开）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "retainHead", value = "是否保留开始的信息，默认：false", dataType = "boolean"),
    })
    @RequestMapping(value = "/recursion_list.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage recursionList(@RequestParam("startIds") String startIds
            , @RequestParam(defaultValue = "false") boolean retainHead) {
        try {
            String[] split = startIds.split(",");
            return success(appDictionaryAuthorityService.recursiveQuery(Arrays.asList(split), retainHead));
        } catch (Exception e) {
            return fail("数据获取失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "更具父ID获取字典数据", notes = "更具父ID获取字典数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "父ID", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage list(@RequestParam("parentId") String parentId) {
        try {
            return success(appDictionaryAuthorityService.selectPageInfoByParam(new Where("parentId", parentId, Where.FieldType.LONG), null, null).getList());
        } catch (Exception e) {
            return fail("数据获取失败：" + e.getMessage());
        }
    }

}
